<?php


namespace app\controller;


use app\BaseController;
use app\model\User;
use app\Request;
use think\facade\Db;
use think\facade\Filesystem;

class UserController extends  BaseController
{
    public function insertUser(Request $request){
        $param = $request->param(['username','pwd','repeatpwd','email']);
        $headImg = $request->file('headImg');

        $this->validate($param,'UserValidate.insertUser');
        if ($param['pwd'] != $param['repeatpwd']){
            return $this->returnJson(self::USER_ERROR,'请输入正确的密码');
        }

        // 验证
        if (empty($headImg)){
            return $this->returnJson(self::USER_ERROR,'请上传头像');
        }
        if (!$headImg->getSize() == 8000000){
            return $this->returnJson(self::USER_ERROR,'上传头像过大');
        }
        if (!in_array($headImg->extension(),$this->mime())){
            return $this->returnJson(self::USER_ERROR,'请上传正确的图片格式');
        }

        if (User::where('email' ,'=', $param['email'])->count() != 0 ){
            return $this->returnJson(self::USER_ERROR,'该用户已存在');
        }

        $root = $request->server('HTTP_HOST') .DIRECTORY_SEPARATOR . 'storage'.DIRECTORY_SEPARATOR ;
        $path = DIRECTORY_SEPARATOR . 'headImg'  ;

        Db::startTrans();
        try {
            $fileUrl = Filesystem::disk('public')->putFile($path,$headImg,'uniqid');
            User::insert([
                'username' => $param['username'],
                'email' => $param['email'],
                'headImg' => 'http://'. $root . $fileUrl,
                'password' => md5($param['pwd']),
            ]);

            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            return $this->returnJson(self::SERVER_ERROR,'系统错误',$exception);
        }
        return $this->returnJson(self::SUCCESS,'cg');
    }

    public function getUser(Request $request){
        return $this->returnJson(self::SUCCESS,'cg',User::where('id','=',$request->user_id)->find());
    }

    public function updateUser(Request $request){
        $user_id = $request->user_id;

        $param = $request->put();

        $updateList=array();
        $msg='';

        if (!empty($param['pwd'])){
            $this->validate($param,'UserValidate.updatePwd');
            if (md5($param['oldpwd']) != User::where('id','=',$user_id)->find()['password']){
                return $this->returnJson(self::USER_ERROR,'请输入正确的旧密码');
            }
            $updateList['password'] =md5($param['pwd']);
            $msg = '密码';
        }
        if (!empty($param['username'])){
            $this->validate($param,'UserValidate.updateUsername');
            $updateList['username'] =$param['username'];
            $msg = '用户名';
        }

        User::where('id','=',$user_id)->update($updateList);
        return $this->returnJson(self::SUCCESS,'修改'.$msg.'成功');

    }

    public function updateHeadImg(Request $request){
        $user_id = $request->user_id;

        $headImg = $request->file('headImg');

        // 验证
        if (empty($headImg)){
            return $this->returnJson(self::USER_ERROR,'请上传头像');
        }
        if (!$headImg->getSize() == 8000000){
            return $this->returnJson(self::USER_ERROR,'上传头像过大');
        }
        if (!in_array($headImg->extension(),$this->mime())){
            return $this->returnJson(self::USER_ERROR,'请上传正确的图片格式');
        }
        $root = $request->server('HTTP_HOST') .DIRECTORY_SEPARATOR . 'storage'.DIRECTORY_SEPARATOR ;
        $path = DIRECTORY_SEPARATOR . 'headImg'  ;
        Db::startTrans();
        try {
            $fileUrl = Filesystem::disk('public')->putFile($path,$headImg,'uniqid');
            User::where('id','=',$user_id)->update(['headImg'=>'http://'.$root . $fileUrl,]);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            return $this->returnJson(self::SERVER_ERROR,'系统错误',$exception);
        }
        return $this->returnJson(self::SUCCESS,'修改头像成功');
    }
}